<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Redis AOF 重写机制深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f8fafc;
            color: #1e293b;
            line-height: 1.6;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .highlight-text {
            background: linear-gradient(90deg, #f59e0b, #ef4444);
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
        }
        .concept-node {
            transition: all 0.3s ease;
        }
        .concept-node:hover {
            transform: scale(1.05);
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
        }
        .first-letter {
            float: left;
            font-size: 5rem;
            line-height: 0.8;
            margin: 0.15em 0.1em 0 0;
            color: #4f46e5;
            font-weight: 700;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6 sm:px-12 lg:px-24">
        <div class="max-w-6xl mx-auto">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold mb-6 leading-tight">
                        Redis <span class="highlight-text">AOF 重写</span>机制
                    </h1>
                    <p class="text-xl md:text-2xl font-light mb-8 opacity-90">
                        持久化性能优化的核心秘密
                    </p>
                    <div class="flex items-center space-x-4">
                        <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm font-medium">
                            <i class="fas fa-database mr-2"></i>Redis 7.0
                        </span>
                        <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm font-medium">
                            <i class="fas fa-bolt mr-2"></i>高性能
                        </span>
                    </div>
                </div>
                <div class="md:w-1/2 flex justify-center">
                    <div class="relative w-full max-w-md">
                        <div class="absolute -top-10 -left-10 w-32 h-32 bg-purple-300 rounded-full filter blur-2xl opacity-50"></div>
                        <div class="absolute -bottom-10 -right-10 w-32 h-32 bg-pink-300 rounded-full filter blur-2xl opacity-50"></div>
                        <div class="relative bg-white bg-opacity-10 backdrop-filter backdrop-blur-lg rounded-2xl p-8 shadow-2xl border border-white border-opacity-20">
                            <div class="flex flex-col space-y-4">
                                <div class="flex items-center">
                                    <div class="w-4 h-4 bg-red-500 rounded-full mr-3"></div>
                                    <span class="font-medium">主进程继续处理命令</span>
                                </div>
                                <div class="flex items-center">
                                    <div class="w-4 h-4 bg-green-500 rounded-full mr-3"></div>
                                    <span class="font-medium">子进程创建新AOF文件</span>
                                </div>
                                <div class="flex items-center">
                                    <div class="w-4 h-4 bg-blue-500 rounded-full mr-3"></div>
                                    <span class="font-medium">文件替换保证一致性</span>
                                </div>
                            </div>
                            <div class="mt-8">
                                <div class="h-1 bg-white bg-opacity-30 rounded-full mb-2"></div>
                                <div class="flex justify-between text-xs text-white text-opacity-70">
                                    <span>原AOF文件</span>
                                    <span>重写过程</span>
                                    <span>新AOF文件</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <section class="py-16 px-6 sm:px-12 lg:px-24">
        <div class="max-w-4xl mx-auto">
            <div class="relative mb-16">
                <h2 class="text-3xl font-bold mb-6 text-gray-800 relative inline-block">
                    <span class="absolute -bottom-2 left-0 w-1/3 h-1 bg-gradient-to-r from-purple-500 to-pink-500 rounded-full"></span>
                    AOF 重写机制深度解析
                </h2>
                
                <p class="text-gray-600 leading-relaxed">
                    <span class="first-letter">A</span>OF 重写是 Redis 维护和优化 AOF 文件的关键过程。通过压缩和清理 AOF 文件，它能够减少文件大小、提高恢复速度和改善性能。这是确保 Redis 持久化系统高效且可靠运行的重要机制。
                </p>
            </div>

            <!-- AOF文件增长 -->
            <div class="mb-16">
                <div class="flex items-center mb-6">
                    <div class="w-12 h-12 rounded-lg bg-purple-100 flex items-center justify-center mr-4">
                        <i class="fas fa-chart-line text-purple-600 text-xl"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-gray-800">1. AOF 文件增长</h3>
                </div>
                
                <div class="grid md:grid-cols-2 gap-8">
                    <div class="bg-white p-6 rounded-xl shadow-sm card-hover">
                        <div class="flex items-center mb-4">
                            <div class="w-8 h-8 rounded-full bg-blue-100 flex items-center justify-center mr-3">
                                <i class="fas fa-file-alt text-blue-500"></i>
                            </div>
                            <h4 class="text-lg font-semibold text-gray-800">AOF 文件的作用</h4>
                        </div>
                        <p class="text-gray-600">
                            AOF 文件记录了 Redis 执行的所有写命令，确保了数据的持久化。每当有写操作发生时，Redis 会将对应的命令追加到 AOF 文件中。
                        </p>
                    </div>
                    
                    <div class="bg-white p-6 rounded-xl shadow-sm card-hover">
                        <div class="flex items-center mb-4">
                            <div class="w-8 h-8 rounded-full bg-red-100 flex items-center justify-center mr-3">
                                <i class="fas fa-expand-alt text-red-500"></i>
                            </div>
                            <h4 class="text-lg font-semibold text-gray-800">文件大小增长</h4>
                        </div>
                        <p class="text-gray-600">
                            随着时间的推移，AOF 文件会不断增长。这会导致文件变得非常大，从而影响加载速度、磁盘空间和 I/O 性能。
                        </p>
                    </div>
                </div>
            </div>

            <!-- AOF重写目的 -->
            <div class="mb-16">
                <div class="flex items-center mb-6">
                    <div class="w-12 h-12 rounded-lg bg-indigo-100 flex items-center justify-center mr-4">
                        <i class="fas fa-bullseye text-indigo-600 text-xl"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-gray-800">2. AOF 重写的目的</h3>
                </div>
                
                <div class="mb-8">
                    <p class="text-gray-600 mb-6">
                        <strong class="text-indigo-600">AOF 重写（AOF Rewrite）</strong> 是一个优化过程，用于压缩和清理 AOF 文件。主要目的包括：
                    </p>
                    
                    <div class="grid md:grid-cols-3 gap-6">
                        <div class="bg-white p-6 rounded-xl shadow-sm card-hover">
                            <div class="flex items-center mb-4">
                                <div class="w-10 h-10 rounded-full bg-purple-100 flex items-center justify-center mr-3">
                                    <i class="fas fa-compress-arrows-alt text-purple-500"></i>
                                </div>
                                <h4 class="text-lg font-semibold text-gray-800">减少文件大小</h4>
                            </div>
                            <p class="text-gray-600 text-sm">
                                AOF 文件中可能包含重复的或过时的命令。重写过程会根据当前数据库的状态生成一个新的 AOF 文件，只包含最简洁的命令集。
                            </p>
                        </div>
                        
                        <div class="bg-white p-6 rounded-xl shadow-sm card-hover">
                            <div class="flex items-center mb-4">
                                <div class="w-10 h-10 rounded-full bg-green-100 flex items-center justify-center mr-3">
                                    <i class="fas fa-tachometer-alt text-green-500"></i>
                                </div>
                                <h4 class="text-lg font-semibold text-gray-800">提高恢复速度</h4>
                            </div>
                            <p class="text-gray-600 text-sm">
                                较小的 AOF 文件意味着 Redis 在重启时加载 AOF 文件的速度更快。重写后的 AOF 文件只包含必要的命令，因此恢复速度更快。
                            </p>
                        </div>
                        
                        <div class="bg-white p-6 rounded-xl shadow-sm card-hover">
                            <div class="flex items-center mb-4">
                                <div class="w-10 h-10 rounded-full bg-blue-100 flex items-center justify-center mr-3">
                                    <i class="fas fa-rocket text-blue-500"></i>
                                </div>
                                <h4 class="text-lg font-semibold text-gray-800">改善性能</h4>
                            </div>
                            <p class="text-gray-600 text-sm">
                                较小的 AOF 文件减少了磁盘 I/O 操作，这有助于提高 Redis 的性能。压缩后的 AOF 文件减少了内存占用。
                            </p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- AOF重写实现 -->
            <div class="mb-16">
                <div class="flex items-center mb-6">
                    <div class="w-12 h-12 rounded-lg bg-pink-100 flex items-center justify-center mr-4">
                        <i class="fas fa-cogs text-pink-600 text-xl"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-gray-800">3. AOF 重写的实现</h3>
                </div>
                
                <div class="mb-8">
                    <p class="text-gray-600 mb-8">
                        <strong class="text-pink-600">AOF 重写的过程</strong> 通常包括以下步骤：
                    </p>
                    
                    <div class="space-y-8">
                        <!-- 步骤1 -->
                        <div class="flex flex-col md:flex-row gap-6">
                            <div class="md:w-1/4">
                                <div class="bg-pink-100 text-pink-600 w-16 h-16 rounded-lg flex items-center justify-center text-2xl font-bold mx-auto">
                                    1
                                </div>
                            </div>
                            <div class="md:w-3/4 bg-white p-6 rounded-xl shadow-sm card-hover">
                                <h4 class="text-lg font-semibold text-gray-800 mb-3">触发重写</h4>
                                <ul class="text-gray-600 space-y-2">
                                    <li class="flex items-start">
                                        <span class="text-pink-500 mr-2">•</span>
                                        <span><strong>自动触发</strong>：Redis 定期检查 AOF 文件的大小和其他条件。如果 AOF 文件的大小超过了配置的阈值（例如，文件大小是最后一次重写后的两倍），Redis 会自动触发重写操作。</span>
                                    </li>
                                    <li class="flex items-start">
                                        <span class="text-pink-500 mr-2">•</span>
                                        <span><strong>手动触发</strong>：管理员也可以通过配置命令手动触发重写过程。</span>
                                    </li>
                                </ul>
                            </div>
                        </div>
                        
                        <!-- 步骤2 -->
                        <div class="flex flex-col md:flex-row gap-6">
                            <div class="md:w-1/4">
                                <div class="bg-purple-100 text-purple-600 w-16 h-16 rounded-lg flex items-center justify-center text-2xl font-bold mx-auto">
                                    2
                                </div>
                            </div>
                            <div class="md:w-3/4 bg-white p-6 rounded-xl shadow-sm card-hover">
                                <h4 class="text-lg font-semibold text-gray-800 mb-3">创建新 AOF 文件</h4>
                                <ul class="text-gray-600 space-y-2">
                                    <li class="flex items-start">
                                        <span class="text-purple-500 mr-2">•</span>
                                        <span><strong>子进程生成</strong>：Redis 会启动一个子进程来生成新的 AOF 文件。子进程会扫描当前数据库的状态，并将所有当前数据库中的数据序列化为一系列命令，写入新的 AOF 文件。</span>
                                    </li>
                                    <li class="flex items-start">
                                        <span class="text-purple-500 mr-2">•</span>
                                        <span><strong>逐步重写</strong>：在子进程重写过程中，Redis 主进程会继续处理来自客户端的命令，并将它们追加到旧的 AOF 文件中。这确保了新 AOF 文件的生成不会影响 Redis 的正常运行。</span>
                                    </li>
                                </ul>
                            </div>
                        </div>
                        
                        <!-- 步骤3 -->
                        <div class="flex flex-col md:flex-row gap-6">
                            <div class="md:w-1/4">
                                <div class="bg-indigo-100 text-indigo-600 w-16 h-16 rounded-lg flex items-center justify-center text-2xl font-bold mx-auto">
                                    3
                                </div>
                            </div>
                            <div class="md:w-3/4 bg-white p-6 rounded-xl shadow-sm card-hover">
                                <h4 class="text-lg font-semibold text-gray-800 mb-3">替换旧 AOF 文件</h4>
                                <ul class="text-gray-600 space-y-2">
                                    <li class="flex items-start">
                                        <span class="text-indigo-500 mr-2">•</span>
                                        <span><strong>交换文件</strong>：当子进程完成重写并且新的 AOF 文件创建完成后，Redis 会将新的 AOF 文件替换旧的 AOF 文件。</span>
                                    </li>
                                    <li class="flex items-start">
                                        <span class="text-indigo-500 mr-2">•</span>
                                        <span><strong>清理</strong>：旧的 AOF 文件会被删除，新的 AOF 文件将用于后续的持久化操作。</span>
                                    </li>
                                </ul>
                            </div>
                        </div>
                        
                        <!-- 步骤4 -->
                        <div class="flex flex-col md:flex-row gap-6">
                            <div class="md:w-1/4">
                                <div class="bg-blue-100 text-blue-600 w-16 h-16 rounded-lg flex items-center justify-center text-2xl font-bold mx-auto">
                                    4
                                </div>
                            </div>
                            <div class="md:w-3/4 bg-white p-6 rounded-xl shadow-sm card-hover">
                                <h4 class="text-lg font-semibold text-gray-800 mb-3">同步与确认</h4>
                                <ul class="text-gray-600 space-y-2">
                                    <li class="flex items-start">
                                        <span class="text-blue-500 mr-2">•</span>
                                        <span><strong>数据一致性</strong>：在重写完成后，Redis 确保新 AOF 文件的一致性和完整性。如果在重写过程中出现问题，Redis 会保留旧的 AOF 文件以防万一。</span>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 配置和优化 -->
            <div class="mb-16">
                <div class="flex items-center mb-6">
                    <div class="w-12 h-12 rounded-lg bg-amber-100 flex items-center justify-center mr-4">
                        <i class="fas fa-sliders-h text-amber-600 text-xl"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-gray-800">4. 配置和优化</h3>
                </div>
                
                <div class="grid md:grid-cols-3 gap-6">
                    <div class="bg-white p-6 rounded-xl shadow-sm card-hover">
                        <div class="flex items-center mb-4">
                            <div class="w-10 h-10 rounded-full bg-purple-100 flex items-center justify-center mr-3">
                                <i class="fas fa-percentage text-purple-500"></i>
                            </div>
                            <h4 class="text-lg font-semibold text-gray-800">auto-aof-rewrite-percentage</h4>
                        </div>
                        <p class="text-gray-600 text-sm">
                            控制自动重写的触发条件，设置一个百分比来确定是否需要触发重写操作。
                        </p>
                    </div>
                    
                    <div class="bg-white p-6 rounded-xl shadow-sm card-hover">
                        <div class="flex items-center mb-4">
                            <div class="w-10 h-10 rounded-full bg-green-100 flex items-center justify-center mr-3">
                                <i class="fas fa-ruler text-green-500"></i>
                            </div>
                            <h4 class="text-lg font-semibold text-gray-800">auto-aof-rewrite-min-size</h4>
                        </div>
                        <p class="text-gray-600 text-sm">
                            设置 AOF 重写的最小文件大小，只有当文件大小超过此值时才会考虑重写。
                        </p>
                    </div>
                    
                    <div class="bg-white p-6 rounded-xl shadow-sm card-hover">
                        <div class="flex items-center mb-4">
                            <div class="w-10 h-10 rounded-full bg-blue-100 flex items-center justify-center mr-3">
                                <i class="fas fa-sync-alt text-blue-500"></i>
                            </div>
                            <h4 class="text-lg font-semibold text-gray-800">aof-rewrite-incremental-fsync</h4>
                        </div>
                        <p class="text-gray-600 text-sm">
                            控制 AOF 重写期间的文件同步策略，以平衡性能和数据安全性。
                        </p>
                    </div>
                </div>
            </div>

            <!-- 可视化图表 -->
            <div class="mb-16">
                <div class="flex items-center mb-6">
                    <div class="w-12 h-12 rounded-lg bg-emerald-100 flex items-center justify-center mr-4">
                        <i class="fas fa-project-diagram text-emerald-600 text-xl"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-gray-800">AOF 重写流程示意图</h3>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-sm">
                    <div class="mermaid">
                        graph TD
                            A[开始] --> B{检查AOF条件}
                            B -->|满足条件| C[触发重写]
                            B -->|不满足| D[继续正常运行]
                            C --> E[创建子进程]
                            E --> F[生成新AOF文件]
                            F --> G[主进程继续处理命令]
                            G --> H[命令追加到新旧AOF文件]
                            F --> I[完成新AOF文件]
                            I --> J[替换旧AOF文件]
                            J --> K[完成重写]
                    </div>
                </div>
            </div>
        </div>
    </section>

    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>